package com.food;


import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.food.zoom.TouchImageView;


import android.R.string;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class menu extends Activity{

private String kategori;
private LocationManager lm;
private int tlfnr;
private Button newMenu = null;
private static final String TAG = "Menu";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu);
	    
		newMenu = (Button) this.findViewById(R.id.but_newMenu);
		
		Intent i = getIntent();
		Bundle b2 = i.getExtras();
		final String text = b2.getString("kategori");
		final String postnr = b2.getString("post");
		kategori=b2.getString("kategori");
		Log.v(TAG, text);
		Log.v(TAG, postnr);
		
		
		newMenu.setOnClickListener(new View.OnClickListener() 
	    {
			@Override
			public void onClick(View v) 
			{
				doStuff(text);
			}
	    });
		Button call = (Button) this.findViewById(R.id.but_call);
		call.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				 Intent callIntent = new Intent(Intent.ACTION_CALL);
				    callIntent.setData(Uri.parse(Integer.toString(tlfnr)));
				    startActivity(callIntent);
				
			}
		});
		doStuff(text);
	}
	
	private void doStuff(String text)
	{
		Log.v(TAG, text);
		final ProgressDialog dialog = ProgressDialog.show(menu.this, "", "Loading. Please    wait...", true);
		dialog.show();
		Handler handler = new Handler();
		Log.v(TAG, "Start delay");
		Runnable re = new Runnable()
		{
			@Override
			public void run() 
			{
				Log.v(TAG, "Delay run");
				getMenu();
		        dialog.dismiss();
		        Log.v(TAG, "Delay end");
			}
			
		};
		handler.postDelayed(re, 1000);
	}
	
	private void getMenu()
	{
		TouchImageView i = (TouchImageView) this.findViewById(R.id.imageView1);
		
		TextView leveringspris = (TextView) this.findViewById(R.id.TextViewLeveringspris);
		
		lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		
		i.setFocusable(true);
		
		//Getting connection and getting the restaurant info
		DBConnection db = new DBConnection();
		String send = 
				"Select TOP 1 Res_Kat.ID, tlfnr, longitude, latitude, ImagePic, " +
				"Leveringspris from Res_Kat,Restaurant " +
				"where Res_Kat.Kategori = '"+kategori+"' " +
				"AND Res_Kat.ID = Restaurant.ID " +
				"ORDER BY NEWID()";
		Log.v(TAG,send);
		Restaurant r = db.getRes(send);
		tlfnr = 31613352; // Change to r.getTlfnr();
		leveringspris.setText(Float.toString(r.getLeveringspris()));
	
		//setLocation(r);
	    i.initialize(r.getImagePic());
	    Log.v(TAG,"getMenu() Ended");
	}

	private void setLocation(Restaurant r)
	{
		TextView t = (TextView) this.findViewById(R.id.TextViewAfstand);
		Location l = new Location("Restaurant");
		l.setLatitude(r.getLatitude());
		l.setLongitude(r.getLongitude());
		
		lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, onLocationChange);  

		Location currentLoc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		Log.v("TAG",l.getLongitude() + " " + l.getLatitude());
		Log.v("TAG",currentLoc.getLongitude() + " " + currentLoc.getLatitude());
		
		Float distance = currentLoc.distanceTo(l);
		Float d2 = distance/1000;
		t.setText(Float.toString(d2.shortValue()) + "km");
		
		Log.v("TAG", Float.toString(distance));
	}
	
	android.location.LocationListener onLocationChange = new android.location.LocationListener() 
    {
        public void onLocationChanged(Location fix) {
          
        }
        
        public void onProviderDisabled(String provider) {
          // required for interface, not used
        }
        
        public void onProviderEnabled(String provider) {
          // required for interface, not used
        }
        
        public void onStatusChanged(String provider, int status,
                                      Bundle extras) {
          // required for interface, not used
        }
      };
}
